package cn.coderliu.config;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class RealmConfig extends AuthorizingRealm {


    /**
     *
     *  用户授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//		 //获取认证时候添加到SimpleAuthenticationInfo中的实例
//		 User user=(User)principals.getPrimaryPrincipal();
//
//		List<String> roleList= roleService.selectRoleByUid(user.getUid());//角色集合
//		List<String> authList=new ArrayList<>();						  //权限集合
//		for(String rid:roleList){
//			List<String> auth= authService.selectAuthByroleid(rid);
//			for(String authUrl:auth){
//				authList.add(authUrl);
//			}
//		}
//		Set<String> role = new HashSet<>();
//    	Set<String> permission = new HashSet<>(authList.size());
//			role.addAll(roleList);
//			permission.addAll(authList);
            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//			authorizationInfo.setRoles(role);
//			authorizationInfo.setStringPermissions(permission);

            return authorizationInfo;
    }


    /**
     *
     * 用户验证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upt = (UsernamePasswordToken) token;
        String username = upt.getUsername();//是否是一样得效果待定：String userName = (String)token.getPrincipal();
//        User usertoken=userService.selectByUsername(username);
//        if(usertoken == null){
//            return null;
//        }
//        //添加角色和权限
//        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo (
//                usertoken,
//                usertoken.getPassword(), //密码
//                ""//salt=username+salt ByteSource.Util.bytes(bouy.getCredentialsSalt())
//        );
//
//        return authenticationInfo;
        return null;
    }
}
